/*
Copyright (c) 2008 André Luiz Alves Moraes

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */

package andre.grids.util.log;

import java.util.*;

import andre.grids.db.dataclasses.*;
import andre.grids.db.*;

/**
 *
 * @author andre
 */
public class Log {
    public static final String OP_TYPE_CREATE = "CREATE";
    public static final String OP_TYPE_DELETE = "DELETE";
    public static final String OP_TYPE_ALTER = "ALTER";
    public static final String OP_TYPE_QUERY = "QUERY";
    public static final String OP_TYPE_RMI_REGISTER = "RMI-REGISTER";
    public static final String OP_TYPE_SYNC = "SYNC";
    
    public static void logException(Exception e, Object sender)
    {
        System.out.println(e.toString());
        System.out.println(sender.getClass().getCanonicalName());
        System.out.println(sender);
        
        e.printStackTrace();
    }
    
    public static void logException(Exception e, Object sender, boolean abort)
    {
        logException(e, sender);
        if (abort)
            System.exit(1);
    }
    
    public static void logNormalOperation(String opType, String opDescription, Object sender) {
        Date now = new java.util.Date(System.currentTimeMillis());
        System.out.println("[" + now.toString() + "]\t[" + opType + "]\t"+opDescription+ (sender==null?"":"\t gerada por: " + sender.getClass().getCanonicalName() + "\t" + sender.toString()));
    }
    
    public static void debug(String message) {
        System.err.println("dbg:[" + (new java.util.Date(System.currentTimeMillis()).toString())+"]\t"+ message);
    }
    
    public static final void logFileCreation(String guidValue, String master, String fileName, String parentName, String[] hostsName) {
        DbLogEntry entry = new DbLogEntry();
        entry.setEntryType(DbLogEntry.CREATE_FILE);
        entry.setEntryDate(new Date(System.currentTimeMillis()));
        entry.setEntryValue(guidValue);
        entry.setOwnerHostName(master);
        entry.setDirLog(true);
        
        StringBuilder builder = new StringBuilder();
        for(String s: hostsName)
            builder.append(s + ":");
        builder.delete(builder.length()-1, builder.length()-1);
        
        // monta uma string na forma: <nome do arquivo>:<nome do diretorio parent>:<1{<nome dos hosts com replicas>}*>
        // ----------------------------------
        entry.setEntryData(fileName+":"+parentName+":"+builder.toString());
                
        Database.getGridFsDb().storeObjectTree(entry);
    }
    
    public static final void logDirectoryCreation(String dirName, String master) {
        DbLogEntry entry = new DbLogEntry();
        entry.setEntryType(DbLogEntry.CREATE_DIRECTORY);
        entry.setEntryDate(new Date(System.currentTimeMillis()));
        entry.setEntryValue(dirName);
        entry.setOwnerHostName(master);
        entry.setDirLog(true);
        Database.getGridFsDb().storeObjectTree(entry);
    }
    
    public static final void logUpdateFile(String guid, String master, long startPos, byte[] rawData) {
        DbLogEntry entry = new DbLogEntry();
        entry.setEntryType(DbLogEntry.UPDATE_FILE);
        entry.setEntryDate(new Date(System.currentTimeMillis()));
        entry.setEntryValue(guid);
        entry.setOwnerHostName(master);
        entry.setDirLog(false);
        entry.setEntryData(startPos + "");
        entry.setRawData(rawData);
        Database.getGridFsDb().storeObjectTree(entry);
    }
    
    public static final void logDeleteFile(String guid, String master) {
        DbLogEntry entry = new DbLogEntry();
        entry.setEntryType(DbLogEntry.DELETE_FILE);
        entry.setEntryDate(new Date(System.currentTimeMillis()));
        entry.setEntryValue(guid);
        entry.setOwnerHostName(master);
        entry.setDirLog(true);
        Database.getGridFsDb().storeObjectTree(entry);        
    }
    
    public static final void logDeleteDirectory(String dirName, String master) {
        DbLogEntry entry = new DbLogEntry();
        entry.setEntryType(DbLogEntry.DELETE_DIRECTORY);
        entry.setEntryDate(new Date(System.currentTimeMillis()));
        entry.setEntryValue(dirName);
        entry.setOwnerHostName(master);
        entry.setDirLog(true);
        Database.getGridFsDb().storeObjectTree(entry);                
    }
}
